package com.sys.dto;

import com.sys.dto.idto.IUserDTO;
import com.sys.entity.ProductEntity;
import com.sys.entity.UserEntity;
import jakarta.persistence.*;


import java.util.List;

public class UserDTO implements IUserDTO {



    @Override
    public void save(UserEntity entity) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        em.persist(entity);
        em.getTransaction().commit();
    }



    @Override
    public void delete(Integer id) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();
        UserEntity userEntity;

        EntityTransaction transaction = em.getTransaction();
        transaction.begin();
        try {
            userEntity = em.find(UserEntity.class, id);
            em.remove(userEntity);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
            e.printStackTrace();
        } finally {
            em.close();
        }
    }

    @Override
    public UserEntity update(UserEntity entity,Integer uid) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();

        UserEntity oldentity = em.find(UserEntity.class, uid);
        EntityTransaction transaction = em.getTransaction();
        if (entity != null) {
            transaction.begin();
            try {
                System.out.println("即将修改的用户: " + oldentity.getUsername());
                oldentity.setUsername(entity.getUsername());
                oldentity.setPassword(entity.getPassword());
                oldentity.setAge(entity.getAge());
                em.merge(oldentity);
                em.getTransaction().commit();
            } catch (Exception e) {
                em.getTransaction().rollback();
                e.printStackTrace();
            } finally {
                em.close();
                return oldentity;
            }
        }
        return entity;
    }

    @Override
    public UserEntity findById(Integer id) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();
        UserEntity userEntity;
            userEntity = em.find(UserEntity.class, id);
            if (userEntity != null) {
                System.out.println("查询到的用户: " + userEntity.getUsername());
            } else {
                System.out.println("未找到用户");
            }
        return userEntity;
    }

    @Override
    public UserEntity changePassword(Integer id, String password) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();
        UserEntity entity;

        entity = em.find(UserEntity.class, id);
        if (entity != null) {
            System.out.println("修改密码的用户: " + entity.getUsername());

            //开启事务
            em.getTransaction().begin();
            try{
                entity.setPassword(password);
                em.merge(entity);
                em.getTransaction().commit();
                return entity;
            }catch (Exception e) {
                em.getTransaction().rollback();
                e.printStackTrace();
            } finally {
                em.close();
            }
            return entity;
        }else {
            System.out.println("没有查询到可以修改用户");
            return null;
        }
    }



    @Override
    public List<UserEntity> findByProperty(String propertyName, Object value) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();
        // 使用查询语句来查找用户
        TypedQuery<UserEntity> query = em.createQuery(
                "SELECT u FROM UserEntity u WHERE u."+ propertyName + "= : " + propertyName,
                UserEntity.class
        );
        query.setParameter(propertyName, value);
        List<UserEntity> userEntities;
        try{
            userEntities = query.getResultList();
            return userEntities;
        }catch (NoResultException e){
            System.out.println("没有找到用户");
            return null;
        }finally {
            em.close();
        }
    }

    @Override
    public UserEntity findByName(String name) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();

        // 使用查询语句来查找用户
        TypedQuery<UserEntity> query = em.createQuery(
                "SELECT u FROM UserEntity u WHERE u.username = :username",
                UserEntity.class
        );
        query.setParameter("username", name);

        UserEntity userEntity;
        try {
            userEntity = query.getSingleResult();
            System.out.println("找到的用户: " + userEntity.getUsername());
            return userEntity;
        } catch (NoResultException e) {
            System.out.println("没有找到用户");
            return null;
        }
    }

    @Override
    public List<UserEntity> findByAge(Object age) {
        return null;
    }


    @Override
    public List<UserEntity> findAll() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();

// 使用查询语句来查找所有用户
        TypedQuery<UserEntity> query = em.createQuery(
                "SELECT u FROM UserEntity u",
                UserEntity.class
        );

        List<UserEntity> userEntities = query.getResultList();

        for (UserEntity userEntity : userEntities) {
            System.out.println("找到的用户: " + userEntity.getUsername());
        }

        return userEntities;
    }
}
